package com.jiuyin.dianjing.ui.activity.team;

import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.jiuyin.dianjing.R;
import com.jiuyin.dianjing.adapter.ManagerGridAdapter;
import com.jiuyin.dianjing.api.base.DefaultApiResult;
import com.jiuyin.dianjing.api.enums.ApiEnum;
import com.jiuyin.dianjing.api.interfaces.ApiResultCallback;
import com.jiuyin.dianjing.api.server.ServerApi;
import com.jiuyin.dianjing.app.HelperApplication;
import com.jiuyin.dianjing.event.RefreshEvent;
import com.jiuyin.dianjing.model.TeamMember;
import com.jiuyin.dianjing.ui.activity.base.BaseActivity;
import com.jiuyin.dianjing.ui.fragment.team.TeamMembersFragment;
import com.jiuyin.dianjing.view.GridSectionAverageGapItemDecoration;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;

import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TOKEN;
import static com.jiuyin.dianjing.api.constant.ApiConstant.KEY_TYPE;

/**
 * 设置管理员
 */
public class ManagerSetActivity extends BaseActivity {
    public static final int REQUEST_MANAGER_ADD = 0;
    public static final int REQUEST_MANAGER_REMOVE = 1;
    @BindView(R.id.rv_managers)
    RecyclerView rvManagers;
    private ArrayList<TeamMember> members;
    private ManagerGridAdapter managerGridAdapter;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_manager_set;
    }

    @Override
    protected void initData() {
        members = getIntent().getParcelableArrayListExtra("members");
    }

    @Override
    protected void initView() {
        setTitle("设置管理员");
        ArrayList<TeamMember> managers = new ArrayList<>();
        for (TeamMember teamMember : members) {
            if (teamMember.part == 1) {
                managers.add(teamMember);
            }
        }
        rvManagers.setLayoutManager(new GridLayoutManager(this, 4));
        rvManagers.addItemDecoration(new GridSectionAverageGapItemDecoration(10, 10, 15, 15));
        managerGridAdapter = new ManagerGridAdapter(managers, this);
        managerGridAdapter.setItemClickListener(new ManagerGridAdapter.ItemClickListener() {
            @Override
            public void onPlusClick() {
                Intent intent = new Intent(ManagerSetActivity.this, ManagerSet2Activity.class);
                intent.putParcelableArrayListExtra("members", members);
                intent.putExtra("type", ManagerSet2Activity.TYPE_ADD);
                startActivityForResult(intent, REQUEST_MANAGER_ADD);
            }

            @Override
            public void onRemoveClick() {
                Intent intent = new Intent(ManagerSetActivity.this, ManagerSet2Activity.class);
                intent.putParcelableArrayListExtra("members", managers);
                intent.putExtra("type", ManagerSet2Activity.TYPE_REMOVE);
                startActivityForResult(intent, REQUEST_MANAGER_REMOVE);
            }

            @Override
            public void onManagerClick(int position) {

            }
        });
        rvManagers.setAdapter(managerGridAdapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        if (resultCode == RESULT_OK && intent != null) {
            ArrayList<TeamMember> selected = intent.getParcelableArrayListExtra("selected");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < selected.size(); i++) {
                stringBuilder.append(selected.get(i).member_id);
                if (i < selected.size() - 1) {
                    stringBuilder.append(",");
                }
            }
            HashMap hashMap = new HashMap();
            hashMap.put("memberIds", stringBuilder.toString());
            hashMap.put(KEY_TOKEN, HelperApplication.getToken());
            hashMap.put(KEY_TYPE, requestCode);
            JSONObject data = new JSONObject(hashMap);

            ApiResultCallback requestCallback = new DefaultApiResult() {
                @Override
                public void onSuccess(JsonObject data) {
                    EventBus.getDefault().post(new RefreshEvent(TeamMembersFragment.class.getSimpleName()));
                    finish();
                }
            };
            ServerApi.post(ApiEnum.SET_TEAM_MANAGE, data, null, progress, d -> addDisposable(d), requestCallback);
        }
        super.onActivityResult(requestCode, resultCode, intent);
    }
}
